import { ElementFinder, by, element, $ } from 'protractor';

import AlertPage from './alert-page';
import { click, waitUntilClickable, waitUntilDisplayed, waitUntilHidden } from '../util/utils';

export class UserManagementPage extends AlertPage {
  title: ElementFinder = element(by.id('user-management-page-heading'));
  modifiedDatSort: ElementFinder = element(by.id('modified-date-sort'));

  async activateUser(login: string) {
    const deactivatedButton = element(by.css('table > tbody')).element(by.id(login)).element(by.css('.btn-danger.deactivated'));

    await deactivatedButton.click();
    await waitUntilHidden(deactivatedButton);
  }

  activatedButton(login: string) {
    return element(by.css('table > tbody')).element(by.id(login)).element(by.css('.btn-success.btn-sm'));
  }

  async getDetailsPage(login: string) {
    const detailsButton = element(by.css('table > tbody')).element(by.id(login)).element(by.css('.btn-info.details'));

    await click(detailsButton);

    const detailsPage = new UserManagementDetailsPage();
    await waitUntilDisplayed(detailsPage.title);
    return detailsPage;
  }

  async getEditPage(login: string) {
    const editButton = element(by.css('table > tbody')).element(by.id(login)).element(by.css('.btn-primary.edit'));

    await click(editButton);

    const detailsPage = new UserManagementDetailsPage();
    await waitUntilDisplayed(detailsPage.editTitle);
    return detailsPage;
  }

  async getDeletePage(login: string) {
    const deleteButton = element(by.css('table > tbody')).element(by.id(login)).element(by.css('.btn-danger.delete'));

    await click(deleteButton);

    const deletePage = new UserManagementDeletePage();
    await waitUntilClickable(deletePage.okButton);
    return deletePage;
  }

  async getEmailColumnText(login: string) {
    const emailControl = element(by.css('table > tbody')).element(by.id(login)).element(by.css('.jhi-user-email'));

    await waitUntilDisplayed(emailControl);
    return await emailControl.getText();
  }
}

export class UserManagementDetailsPage extends AlertPage {
  title: ElementFinder = element(by.css('h2.jh-entity-heading'));
  loginDetails: ElementFinder = element.all(by.css('.jh-entity-details > dd > span')).first();

  editTitle: ElementFinder = element(by.css('h2#myUserLabel'));
  email: ElementFinder = $('input#email');
  saveBtn: ElementFinder = element(by.css('.btn-primary'));
}

export class UserManagementDeletePage {
  modal: ElementFinder = element(by.id('removeUser'));
  okButton: ElementFinder = element(by.id('confirm-delete-user'));
}

export class MetricsPage {
  title: ElementFinder = element(by.id('metrics-page-heading'));
}

export class HealthPage {
  title: ElementFinder = element(by.id('health-page-heading'));
}

export class ConfigurationsPage {
  title: ElementFinder = element(by.id('configuration-page-heading'));
}

export class AuditsPage {
  title: ElementFinder = element(by.id('audits-page-heading'));
}

export class LogsPage {
  title: ElementFinder = element(by.id('logs-page-heading'));
}
